package com.hxy.moobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.constant.RedisMessageConstant;
import com.hxy.travel.entity.Result;
import com.hxy.travel.pojo.Order;
import com.hxy.travel.service.OrderService;
import com.hxy.travel.utils.SMSUtilsHZ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private  OrderService orderService;


    @PostMapping("/addOrderInfo.do")
    public Result addOrderInfo(@RequestBody Map orderInfo){
        String telephone = (String) orderInfo.get("telephone");

        String validateCode = (String) orderInfo.get("validateCode");

        String redisCode = jedisPool.getResource().get(telephone);

        if (redisCode ==null || !redisCode.equals(validateCode) ){

            return Result.fail(MessageConstant.VALIDATECODE_ERROR);

        }
        Result result = Result.fail(MessageConstant.ORDERSETTING_FAIL);

        try {

            result = orderService.addOrder(orderInfo);
        }catch (Exception e){

            e.printStackTrace();
            return result;


        }
        if (result.isFlag()){
            String orderDate = (String) orderInfo.get("orderDate");
            try {

                SMSUtilsHZ.sendShortMessage(telephone,orderDate);
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return result;
    }

    @PostMapping("/findById.do")
    public Result findById(Integer id){
        Map map = orderService.findByIdOrderInfo(id);
        return Result.data(map);
    }



}
